﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using eProject_SEM3_SecurityServices.Bussiness;
using eProject_SEM3_SecurityServices.ObjectClass;
using System.Data;
using System.Data.SqlClient;

namespace eProject_SEM3_SecurityServices.DataAccess
{
    public class DService
    {
        public List<OService> getServiceList()
        {
            List<OService> listService = null;
            using (DataTable table = DConnectSQL.ExecuteSelectCommand("GetServiceList", CommandType.StoredProcedure))
            {
                if (table.Rows.Count > 0)
                {
                    listService = new List<OService>();

                    foreach (DataRow row in table.Rows)
                    {
                        OService service = new OService();
                        service.ServiceId = Convert.ToInt32(row["serviceId"]);
                        service.ServiceName = row["serviceName"].ToString();
                        service.ServiceDescription = row["serviceDescription"].ToString();

                        listService.Add(service);
                    }
                }
            }
            return listService;
        }

        public OService getServiceDetails(int serId)
        {
            OService service = null;
            SqlParameter[] parameter = new SqlParameter[]
            {
                new SqlParameter("@serId", serId)
            };

            using (DataTable table = DConnectSQL.ExecuteParamerizedSelectCommand("GetServiceDetails",CommandType.StoredProcedure,parameter))
            {
                if (table.Rows.Count ==1)
                {
                    DataRow row = table.Rows[0];

                    service = new OService();

                    service.ServiceId = Convert.ToInt32(row["serviceId"]);
                    service.ServiceName = row["serviceName"].ToString();
                    service.ServiceDescription = row["serviceDescription"].ToString();
                }
            }
            return service;
        }

        public bool AddNewService(OService service)
        {
            SqlParameter[] parameter = new SqlParameter[]
            {                
                new SqlParameter("Name",service.ServiceName),
                new SqlParameter("Description",service.ServiceDescription)
            };

            return DConnectSQL.ExecuteNonQuery("AddNewService",CommandType.StoredProcedure,parameter);
        }

        public bool UpdateService(OService service)
        {
            SqlParameter[] parameter = new SqlParameter[]
            {
                new SqlParameter("Id",service.ServiceId),
                new SqlParameter("Name",service.ServiceName),
                new SqlParameter("Description",service.ServiceDescription)
            };

            return DConnectSQL.ExecuteNonQuery("UpdateService", CommandType.StoredProcedure, parameter);
        }

        public bool DeleteService(int serId)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@serId", serId)
            };

            return DConnectSQL.ExecuteNonQuery("DeleteService", CommandType.StoredProcedure, parameters);
        }
    }
}